﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CCF20Model;
using WiseLink.RDS.Resource;

namespace CCF20Model.FaultHandler
{
    public class RejectCustomerFaultHandler : FaultHandlerBase
    {
        public RejectCustomerFaultHandler(IFaultHandler faultHandler)
        {
            this.PreviousHandler = faultHandler;
            this.MyVehicle = faultHandler.MyVehicle;
            this.MyFault = faultHandler.MyFault;
            this.ccf20Ent = faultHandler.ccf20Ent;
        }
        public override void ProcessRequest()
        {
            IFaultHandler nextHandler = null;

            if (DateTime.Now - new TimeSpan(31, 0, 0, 0) < MyVehicle.LastContactTime)
            {
                //如果是撞车，要判断当他上次拒绝时，是否有撞车的请求。 如果有，则一起屏蔽撞车，如果没有，则不屏蔽撞车
                if (MyFault.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent))
                {
                    VehicleRepairHistory vrh = MyVehicle.VehicleRepairHistories.OrderByDescending(r => r.OutShopDate).FirstOrDefault();
                    Fault crashfault = vrh.Faults.Where(f => f.FaultType.Contains(WiseLinkRDSStatus.Fault_Type_Urgent) && f.CloseReason != "排除急救").FirstOrDefault();
                    if (crashfault != null)
                    {
                        CloseFault(MyVehicle.LastContactResult);
                        LoggerHelper.LogZICDebug("[拒绝客户]-撞车请求-客户上次拒绝时间距离当前时间小于30天-上次拒绝时包括撞车-屏蔽撞车请求");
                        nextHandler = new CompleteFaultHandler(this);
                    }
                    else
                    {
                        LoggerHelper.LogZICDebug("[拒绝客户]-撞车请求-客户上次拒绝时间距离当前时间小于30天-但上次拒绝时不包括撞车-显示撞车请求");
                        nextHandler = new ProcessMaintenaceHandler(this);
                    }
                }
                else
                {
                    //忽略在过去三十天内，拒绝回场的用户的故障请求，里程（保养）,
                    CloseFault(MyVehicle.LastContactResult);
                    LoggerHelper.LogZICDebug(string.Format("[拒绝客户] {0}-而且客户上次拒绝时间={1}距离当前时间小于30天-所以在30天内不显示故障维修请求",
                                                                  MyVehicle.LastContactResult,
                                                                  MyVehicle.LastContactTime));
                    nextHandler = new CompleteFaultHandler(this);
                }
            }
            else
            {
                LoggerHelper.LogZICDebug(string.Format("[拒绝客户]-{0} 客户上次拒绝时间={1}距离当前时间大于30天-显示客户请求",
                                                             MyVehicle.LastContactResult,
                                                             MyVehicle.LastContactTime));
                nextHandler = new ProcessMaintenaceHandler(this);
            }
            nextHandler.ProcessRequest();
        }
    }
}